home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / msdos / ticker.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  2KB  |  90 lines

  1. #include "mamalleg.h"
  2. #include "driver.h"
  3. #include "ticker.h"
  4. #include <time.h>
  5.  
  6. #define USE_UCLOCK
  7. //#define USE_RDTSC
  8.  
  9. static int use_rdtsc;
  10. TICKER ticks_per_sec;
  11.  
  12. TICKER ticker(void)
  13. {
  14.     if (use_rdtsc)
  15.     {
  16.         INT64 result;
  17.  
  18.         __asm__ __volatile__ (
  19.             "rdtsc"
  20.             : "=A" (result)
  21.         );
  22.  
  23.         return result;
  24.     }
  25.     else
  26.     {
  27.         /* this assumes that uclock_t is 64-bit (which it is) */
  28.         return uclock();
  29.     }
  30. }
  31.  
  32. int cpu_rdtsc(void)
  33. {
  34.     int result;
  35.  
  36.     __asm__ (
  37.         "movl $1,%%eax     ; "
  38.         "xorl %%ebx,%%ebx  ; "
  39.         "xorl %%ecx,%%ecx  ; "
  40.         "xorl %%edx,%%edx  ; "
  41.         "cpuid             ; "
  42.         "testl $0x10,%%edx ; "
  43.         "setne %%al        ; "
  44.         "andl $1,%%eax     ; "
  45.     :  "=&a" (result)   /* the result has to go in eax */
  46.     :       /* no inputs */
  47.     :  "%ebx", "%ecx", "%edx" /* clobbers ebx ecx edx */
  48.     );
  49.     return result;
  50. }
  51.  
  52. void init_ticker(void)
  53. {
  54.     /* if the RDTSC instruction is available use it because */
  55.     /* it is more precise and has less overhead than uclock() */
  56.     if (cpu_cpuid && cpu_rdtsc())
  57.     {
  58.         uclock_t a,b;
  59.         TICKER start,end;
  60.  
  61.         use_rdtsc = 1;    /* must set this before calling ticker() */
  62.  
  63.         a = uclock();
  64.         /* wait some time to let everything stabilize */
  65.         do
  66.         {
  67.             b = uclock();
  68.         } while (b-a < UCLOCKS_PER_SEC/10);
  69.  
  70.         a = uclock();
  71.         start = ticker();
  72.         do
  73.         {
  74.             b = uclock();
  75.         } while (b-a < UCLOCKS_PER_SEC/4);
  76.         end = ticker();
  77.         ticks_per_sec = (end - start)*4;
  78.  
  79. logerror("using RDTSC for timing, CPU speed = %d.%06d MHz\n",
  80.         (int)(ticks_per_sec/1000000),(int)(ticks_per_sec%1000000));
  81.     }
  82.     else
  83.     {
  84.         use_rdtsc = 0;
  85.         ticks_per_sec = UCLOCKS_PER_SEC;
  86.  
  87. logerror("using uclock() for timing\n");
  88.     }
  89. }
  90.